package com.victor.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Jingqulist;
import com.victor.entity.Parameter;
import com.victor.entity.SearchHistory;
import com.victor.entity.finefood.Food;
import com.victor.entity.happy.Happy;
import com.victor.entity.hotel.Hotel;
import com.victor.service.JingquService;
import com.victor.service.PartnersService;
import com.victor.service.SearchhistoryService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/firstpage")
public class FirstPageController {
	@Autowired
	private SearchhistoryService searchhistoryService;
	@Autowired
	private JingquService jingquService;
	@Autowired
	private PartnersService partnerService;
	
	/**
	 * 添加搜索
	 * @param searchHistory
	 * @return
	 */
	@RequestMapping("/addsearch")
	public Msg addSearch(SearchHistory searchHistory) {
		String[] fileld = {"uid","city","search"};
		boolean notEmpty = ClassFilter.somePropertyNotEmpty(searchHistory, fileld);
		if (notEmpty==false) {
			return Msg.fail("入参失败");
		}
		boolean b = searchhistoryService.insertSearch(searchHistory);
		if (!b) {
			return Msg.fail("添加搜索失败");
		}
		return Msg.success();
	}
	/**
	 * 搜索
	 * @param search
	 * @return
	 */
	@RequestMapping("/getsearch")
	public Msg search(String search,int page,int row) {
		if (search == null || search.isEmpty()) {
			return Msg.fail("入参失败");
		}
		HashMap<Object, Object> map=new HashMap<>();
		map.put("search", search);
		PageInfo<Jingqulist> jingqu = jingquService.selByCondition(map,page,row);
		//去三个表里搜索
		PageInfo<Food> food = partnerService.selFood(search,page,row);
		PageInfo<Happy> happy = partnerService.selHappy(search,page,row);
		PageInfo<Hotel> hotelInfo = partnerService.selHotel(search,page,row);
		return Msg.success().add("jingqu", jingqu).add("food", food).add("happy", happy).add("hotelInfo", hotelInfo);
	}
	/**
	 * 历史搜索
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getuidsearch")
	public Msg findAllHistory(String uid) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		List<SearchHistory> list = searchhistoryService.selByUid(uid);
		return Msg.success().add("list", list);
	}
	/**
	 * 搜索推荐(city),热门搜索(null)
	 * @param city
	 * @return
	 */
	@RequestMapping("/gethot")
	public Msg findByCity(String city) {
		Map<Object,Object> map = new HashMap<>();
		if (city != null && !city.isEmpty()) {
			map.put("city", city);
		}
		List<SearchHistory> list = searchhistoryService.selByCity(map);
		return Msg.success().add("list", list);
	}
	

}
